﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BusinessObjects;
using System.Windows.Threading;
using Microsoft.Practices.Composite.Events;
using YourWorld.Common.Infrastucture.Events;
using MainGameModule.ViewModels;

namespace MainGameModule
{
    public class PlayerStatusManager
    {
        private static PlayerStatusManager instance;
        private DispatcherTimer timer;

        public event Action<int> ExperienceChanged;
        public event Action HealthChanged;

        private PlayerStatusManager()
        {
            this.timer = new DispatcherTimer();
            this.timer.Interval = TimeSpan.FromSeconds( 1 );
            this.timer.Tick += new EventHandler( timer_Tick );
        }

        public static PlayerStatusManager Instance
        {
            get
            {
                if ( instance == null )
                    instance = new PlayerStatusManager();

                return instance;
            }
        }

        public int Experience
        {
            get;
            private set;
        }

        public double Health
        {
            get;
            private set;
        }

        public void StartGame()
        {
            this.Health = 200;
            this.timer.Start();
        }

        public void PauseGame()
        {
            this.timer.Stop();
        }

        public void ContinueGame()
        {
            this.timer.Start();
        }

        public void AddExperience( int value )
        {
            this.Experience += value;
            this.OnExperienceChaged( value );
             
            this.AddHealth( value / 2 );
            EventService.Aggregator.GetEvent<AddToExperienceEvent>().Publish( value );
        }

        public void AddHealth( double value )
        {
            this.Health += value;
            this.OnHealthChanged();
        }

        private void OnHealthChanged()
        {
            if ( this.HealthChanged != null )
                this.HealthChanged();
        }

        private void OnExperienceChaged( int value )
        {
            if ( this.ExperienceChanged != null )
                this.ExperienceChanged( value );
        }

        private void timer_Tick( object sender, EventArgs e )
        {
            this.Health -= Consts.PlayerHealthChangeStep;
            this.OnHealthChanged();
        }
    }
}
